<?php
/**
 * Index controller
 *
 * Default controller for this application.
 * 
 * @uses       Zend_Controller_Action
 * @package    FireFeed
 * @subpackage Controller
 */
class IndexController extends Zend_Controller_Action
{
	/**
	 * @var Zend_Auth
	 */
	private $_auth;
	
    /**
     * This will initialize CSS and javascript for the template system
     * Also gets the translator from application registry
     * 
     * @return void
     */
    public function init()
    {
    	$this->_auth = Zend_Auth::getInstance();
        
        if ($this->_auth->hasIdentity()) {
            $this->view->username = 
                $this->_auth->getIdentity()->userProfile->nickname;
        }
        
    	$this->view->headScript()->appendFile('/js/slide.js');
    	$this->view->headScript()->appendFile('/js/jquery.rating.js');
        $this->view->headScript()->appendFile('/js/formjs.js');
        $this->view->headScript()->appendFile('/js/jquery.form.js');
        $this->view->headScript()->appendFile('/js/user.js');
        $this->view->headScript()->appendFile('/js/index.js');
        $this->view->headScript()->appendFile('/js/inettuts.js');
        $this->view->headLink()->appendStylesheet('/css/jquery.rating.css');
        $this->view->headLink()->appendStylesheet('/css/tooltip.css');
        $this->view->headLink()->appendStylesheet('/css/jquery.accordion.css');
        
        //feedtree
        $this->view->headScript()->appendFile('/js/feedtree/jqueryFeedTree.js');
        $this->view->headLink()->appendStylesheet('/css/feedTreeStyle.css');
        
        //greybox
        $this->view->headScript()->appendFile('/js/greybox.js');
        $this->view->headLink()->appendStylesheet('/css/greybox.css');
    }
    
    /**
     * The "index" action is the default action for all controllers
     *
     * Assuming the default route and default router, this action is dispatched 
     * via the following urls:
     * - /
     * - /index/
     * - /index/index
     *
     * It redirects to user home page if any is logged in
     * 
     * @return void
     */
    public function indexAction()
    {
        if ($this->_auth->hasIdentity()) {
            $this->_redirect($this->_auth->getIdentity()->roles->homepage);
            return;
        }
    	
        $this->_helper->layout->setLayout('indexlayout');
        
        $this->view->formLogin = new Form_Login();
    }
    
    /**
     * Loads homepage feed boxes
     * 
     * - Ajax action
     * @return void
     */
    public function loadfeedboxesAction()
    {
    	$this->_helper->layout->disableLayout();
    	
    	$factory = Model_Factory::getInstance();
    	$service = $factory->getBoxService();
    	
    	$boxes = $service->searchBoxes(array('isPublished' => '1'))
    	                 ->getItems(null, null);
    	                 
    	$this->view->boxes = $boxes;
    }
    
    /**
     * Load top ten box
     * - Ajax action
     * 
     * @return void
     */
    public function loadtoptenAction()
    {
    	$this->_helper->layout->disableLayout();
    	
    	$factory = Model_Factory::getInstance();
        $catService = $factory->getCategoryService();
    	$feedService = $factory->getFeedService();
    	
        $categories = $catService->getTopTenCategories();
    	
        foreach($categories as $category) {
    		$category->setFeeds($feedService->getTopTenFeeds($category->oid));
    	}
    	
    	if(Zend_Auth::getInstance()->hasIdentity()) {
    	   $this->view->showAddFeed = true;
    	} else {
    	   $this->view->showAddFeed = false;
    	}
    	
    	$this->view->categories = $categories;
    }
    
    /**
     * User Homepage
     * 
     * @return void
     */
    public function userAction()
    {
    	
    }

    /**
     * Admin Homepage
     * 
     * @return void
     */
    public function adminAction()
    {
    	$this->_helper->layout->disableLayout();
    	$this->_helper->layout->setLayout('adminlayout');
    }
}
